package com.gulimall.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gulimall.common.constant.ProductConstant;
import com.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.AttrAttrgroupRelationService;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.service.CategoryService;
import com.gulimall.product.vo.AttrGroupVo;
import com.gulimall.product.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;

import com.gulimall.product.dao.AttrGroupDao;
import com.gulimall.product.entity.AttrGroupEntity;
import com.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationService relationService;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {

        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.and(obj -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (categoryId == null || categoryId.intValue() == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );

            return new PageUtils(page);
        } else {
            queryWrapper.eq("catelog_id", categoryId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );

            return new PageUtils(page);
        }

    }

    @Override
    public PageUtils queryNoAttrPage(Map<String, Object> params, Long attrgroupId) {
        //attrgroup 主体，手机
        //attr  上市年份，基本属性/销售属性，手机
        //category 手机
        //attr_attrgroup_relation    attrId-attrGroupId 主体-上市年份

        //当前分组所属分类
        //没有被关联
        //基本属性

        AttrGroupEntity attrGroupEntity = this.getById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();

        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<Long> attrGroupIds = attrGroupEntities.stream().map(group -> {
            return group.getAttrGroupId();
        }).collect(Collectors.toList());

        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id", catelogId);
        queryWrapper.eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());

        String key = (String) params.get("key");
        queryWrapper.and(w -> {
            w.eq("attr_id", key).or().like("attr_name", key);
        });

        List<AttrAttrgroupRelationEntity> relations = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", attrGroupIds));
        if (CollectionUtil.isNotEmpty(relations)) {
            List<Long> attrIds = relations.stream().map(relation -> {
                return relation.getAttrId();
            }).collect(Collectors.toList());
            queryWrapper.notIn("attr_id", attrIds);
        }

        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupVo> getAttrGroupWithAttr(Long catelogId) {
        List<AttrGroupVo> groupVos = new ArrayList<>();
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        if (CollectionUtil.isNotEmpty(attrGroupEntities)) {
            groupVos = attrGroupEntities.stream().map(group -> {
                AttrGroupVo attrGroupVo = new AttrGroupVo();
                BeanUtils.copyProperties(group, attrGroupVo);
                List<AttrAttrgroupRelationEntity> relations = relationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", group.getAttrGroupId()));
                if (CollectionUtil.isNotEmpty(relations)) {
                    List<Long> attrIds = relations.stream().map(relation -> {
                        return relation.getAttrId();
                    }).collect(Collectors.toList());
                    Collection<AttrEntity> attrEntities = attrService.listByIds(attrIds);
                    List<AttrVo> attrVos = attrEntities.stream().map(attr -> {
                        AttrVo attrVo = new AttrVo();
                        BeanUtils.copyProperties(attr, attrVo);
                        return attrVo;
                    }).collect(Collectors.toList());
                    attrGroupVo.setAttrs(attrVos);
                }
                return attrGroupVo;
            }).collect(Collectors.toList());
        }
        return groupVos;
    }
}